package test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.th.spider.URLUtil;

public class GetGoogle300Pic {
    
    public List<String> urlList = new ArrayList<String>();
    
    public GetGoogle300Pic() {
        //        this.setUrlList();
        //        try {
        //            this.downloadPic();
        //        } catch (Exception e) {
        //            e.printStackTrace();
        //        }
    }
    
    /**
     * @Description: 设置图片地址
     */
    private void setUrlList() {
        String url = "http://www.google.co.jp/landing/motto/tabplay/";
        for (int i = 19; i <= 43; i++) {
            String tmpUrl = url + i + ".jpg";
            urlList.add(tmpUrl);
        }
    }
    
    /**
     * @throws Exception 
     * @Description: 下载图片
     */
    private void downloadPic() throws Exception {
        if (null != urlList && urlList.size() > 0) {
            String dir = "d:/google/";
            for (String url : urlList) {
                String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
                String file = dir + fileName;
                BufferedOutputStream out = null;
                byte[] bit = URLUtil.getByte(url);
                if (bit.length > 0) {
                    try {
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        out.write(bit);
                        out.flush();
                    } finally {
                        if (out != null)
                            out.close();
                    }
                }
            }
        }
        
    }
    
    public static void downLoadFile(String url, String dir) throws Exception {
        //        http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient-contrib/docs/translated-tutorial/httpclient-tutorial-simplified-chinese.pdf
        
        String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
        String file = dir + fileName;
        BufferedOutputStream out = null;
        byte[] bit = URLUtil.getByte(url);
        if (bit.length > 0) {
            try {
                out = new BufferedOutputStream(new FileOutputStream(file));
                out.write(bit);
                out.flush();
            } finally {
                if (out != null)
                    out.close();
            }
        }
        
    }
    
    /**
     * @Description: 这里用一句话描述这个方法的作用
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(new Date().toLocaleString());
        try {
            GetGoogle300Pic
                    .downLoadFile(
                            "http://vhotwsh.video.qq.com/flv/186/246/9x1lpyTkRXV.mp4?vkey=B5E09E8691D26B7B80E06E9C154B17B5AA5F763A2D2E07FC840075317ACDBADC",
                            "d:/download");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        System.out.println(new Date().toLocaleString());
        //        try {
        //            new FileOutputStream("d:/google/1.jpg");
        //        } catch (FileNotFoundException e) {
        //            // TODO Auto-generated catch block
        //            e.printStackTrace();
        //        }
    }
    
}
